// 省市区三级联动数据工具
const regionData = {
  // 省份数据（按优先级排序：安徽、上海、浙江、江苏）
  provinces: [
    { code: '340000', name: '安徽省' },
    { code: '310000', name: '上海市' },
    { code: '330000', name: '浙江省' },
    { code: '320000', name: '江苏省' },
    { code: '110000', name: '北京市' },
    { code: '120000', name: '天津市' },
    { code: '130000', name: '河北省' },
    { code: '140000', name: '山西省' },
    { code: '150000', name: '内蒙古自治区' },
    { code: '210000', name: '辽宁省' },
    { code: '220000', name: '吉林省' },
    { code: '230000', name: '黑龙江省' },
    { code: '350000', name: '福建省' },
    { code: '360000', name: '江西省' },
    { code: '370000', name: '山东省' },
    { code: '410000', name: '河南省' },
    { code: '420000', name: '湖北省' },
    { code: '430000', name: '湖南省' },
    { code: '440000', name: '广东省' },
    { code: '450000', name: '广西壮族自治区' },
    { code: '460000', name: '海南省' },
    { code: '500000', name: '重庆市' },
    { code: '510000', name: '四川省' },
    { code: '520000', name: '贵州省' },
    { code: '530000', name: '云南省' },
    { code: '540000', name: '西藏自治区' },
    { code: '610000', name: '陕西省' },
    { code: '620000', name: '甘肃省' },
    { code: '630000', name: '青海省' },
    { code: '640000', name: '宁夏回族自治区' },
    { code: '650000', name: '新疆维吾尔自治区' }
  ],

  // 城市数据
  cities: {
    '110000': [{ code: '110100', name: '北京市' }],
    '120000': [{ code: '120100', name: '天津市' }],
    '130000': [
      { code: '130100', name: '石家庄市' },
      { code: '130200', name: '唐山市' },
      { code: '130300', name: '秦皇岛市' },
      { code: '130400', name: '邯郸市' },
      { code: '130500', name: '邢台市' },
      { code: '130600', name: '保定市' },
      { code: '130700', name: '张家口市' },
      { code: '130800', name: '承德市' },
      { code: '130900', name: '沧州市' },
      { code: '131000', name: '廊坊市' },
      { code: '131100', name: '衡水市' }
    ],
    '310000': [{ code: '310100', name: '上海市' }],
    '320000': [
      { code: '320500', name: '苏州市' }, // 苏州排第二
      { code: '320100', name: '南京市' }, // 南京排第三
      { code: '320200', name: '无锡市' },
      { code: '320300', name: '徐州市' },
      { code: '320400', name: '常州市' },
      { code: '320600', name: '南通市' },
      { code: '320700', name: '连云港市' },
      { code: '320800', name: '淮安市' },
      { code: '320900', name: '盐城市' },
      { code: '321000', name: '扬州市' },
      { code: '321100', name: '镇江市' },
      { code: '321200', name: '泰州市' },
      { code: '321300', name: '宿迁市' }
    ],
    '330000': [
      { code: '330500', name: '湖州市' }, // 湖州排第一
      { code: '330100', name: '杭州市' },
      { code: '330200', name: '宁波市' },
      { code: '330300', name: '温州市' },
      { code: '330400', name: '嘉兴市' },
      { code: '330600', name: '绍兴市' },
      { code: '330700', name: '金华市' },
      { code: '330800', name: '衢州市' },
      { code: '330900', name: '舟山市' },
      { code: '331000', name: '台州市' },
      { code: '331100', name: '丽水市' }
    ],
    '340000': [
      { code: '341800', name: '宣城市' }, // 宣城排第二，但包含广德
      { code: '340100', name: '合肥市' },
      { code: '340200', name: '芜湖市' },
      { code: '340300', name: '蚌埠市' },
      { code: '340400', name: '淮南市' },
      { code: '340500', name: '马鞍山市' },
      { code: '340600', name: '淮北市' },
      { code: '340700', name: '铜陵市' },
      { code: '340800', name: '安庆市' },
      { code: '341000', name: '黄山市' },
      { code: '341100', name: '滁州市' },
      { code: '341200', name: '阜阳市' },
      { code: '341300', name: '宿州市' },
      { code: '341500', name: '六安市' },
      { code: '341600', name: '亳州市' },
      { code: '341700', name: '池州市' }
    ]
  },

  // 区县数据
  districts: {
    '110100': [
      { code: '110101', name: '东城区' },
      { code: '110102', name: '西城区' },
      { code: '110105', name: '朝阳区' },
      { code: '110106', name: '丰台区' },
      { code: '110107', name: '石景山区' },
      { code: '110108', name: '海淀区' },
      { code: '110109', name: '门头沟区' },
      { code: '110111', name: '房山区' },
      { code: '110112', name: '通州区' },
      { code: '110113', name: '顺义区' },
      { code: '110114', name: '昌平区' },
      { code: '110115', name: '大兴区' },
      { code: '110116', name: '怀柔区' },
      { code: '110117', name: '平谷区' },
      { code: '110118', name: '密云区' },
      { code: '110119', name: '延庆区' }
    ],
    '120100': [
      { code: '120101', name: '和平区' },
      { code: '120102', name: '河东区' },
      { code: '120103', name: '河西区' },
      { code: '120104', name: '南开区' },
      { code: '120105', name: '河北区' },
      { code: '120106', name: '红桥区' },
      { code: '120110', name: '东丽区' },
      { code: '120111', name: '西青区' },
      { code: '120112', name: '津南区' },
      { code: '120113', name: '北辰区' },
      { code: '120114', name: '武清区' },
      { code: '120115', name: '宝坻区' },
      { code: '120116', name: '滨海新区' },
      { code: '120117', name: '宁河区' },
      { code: '120118', name: '静海区' },
      { code: '120119', name: '蓟州区' }
    ],
    '310100': [
      { code: '310101', name: '黄浦区' },
      { code: '310104', name: '徐汇区' },
      { code: '310105', name: '长宁区' },
      { code: '310106', name: '静安区' },
      { code: '310107', name: '普陀区' },
      { code: '310109', name: '虹口区' },
      { code: '310110', name: '杨浦区' },
      { code: '310112', name: '闵行区' },
      { code: '310113', name: '宝山区' },
      { code: '310114', name: '嘉定区' },
      { code: '310115', name: '浦东新区' },
      { code: '310116', name: '金山区' },
      { code: '310117', name: '松江区' },
      { code: '310118', name: '青浦区' },
      { code: '310120', name: '奉贤区' },
      { code: '310151', name: '崇明区' }
    ],
    '320100': [
      { code: '320102', name: '玄武区' },
      { code: '320104', name: '秦淮区' },
      { code: '320105', name: '建邺区' },
      { code: '320106', name: '鼓楼区' },
      { code: '320111', name: '浦口区' },
      { code: '320113', name: '栖霞区' },
      { code: '320114', name: '雨花台区' },
      { code: '320115', name: '江宁区' },
      { code: '320116', name: '六合区' },
      { code: '320117', name: '溧水区' },
      { code: '320118', name: '高淳区' }
    ],
    '320500': [
      { code: '320505', name: '虎丘区' },
      { code: '320506', name: '吴中区' },
      { code: '320507', name: '相城区' },
      { code: '320508', name: '姑苏区' },
      { code: '320509', name: '吴江区' },
      { code: '320581', name: '常熟市' },
      { code: '320582', name: '张家港市' },
      { code: '320583', name: '昆山市' },
      { code: '320585', name: '太仓市' }
    ],
    '330100': [
      { code: '330102', name: '上城区' },
      { code: '330105', name: '拱墅区' },
      { code: '330106', name: '西湖区' },
      { code: '330108', name: '滨江区' },
      { code: '330109', name: '萧山区' },
      { code: '330110', name: '余杭区' },
      { code: '330111', name: '富阳区' },
      { code: '330112', name: '临安区' },
      { code: '330113', name: '临平区' },
      { code: '330114', name: '钱塘区' },
      { code: '330122', name: '桐庐县' },
      { code: '330127', name: '淳安县' },
      { code: '330182', name: '建德市' }
    ],
    '330500': [
      { code: '330502', name: '吴兴区' },
      { code: '330503', name: '南浔区' },
      { code: '330521', name: '德清县' },
      { code: '330522', name: '长兴县' },
      { code: '330523', name: '安吉县' }
    ],
    '340100': [
      { code: '340102', name: '瑶海区' },
      { code: '340103', name: '庐阳区' },
      { code: '340104', name: '蜀山区' },
      { code: '340111', name: '包河区' },
      { code: '340121', name: '长丰县' },
      { code: '340122', name: '肥东县' },
      { code: '340123', name: '肥西县' },
      { code: '340124', name: '庐江县' },
      { code: '340181', name: '巢湖市' }
    ],
    '341800': [
      { code: '341822', name: '广德市' }, // 广德排第一
      { code: '341881', name: '宁国市' }, // 宁国排第二
      { code: '341821', name: '郎溪县' }, // 郎溪排第三
      { code: '341802', name: '宣州区' },
      { code: '341823', name: '泾县' },
      { code: '341824', name: '绩溪县' },
      { code: '341825', name: '旌德县' }
    ]
  }
};

// 常见地址快速选择
const commonAddresses = {
  // 籍贯常见地址
  hometown: [
    { province: '安徽省', city: '宣城市', district: '广德市', fullName: '广德市' }
  ],
  // 现居地常见地址
  residence: [
    { province: '安徽省', city: '宣城市', district: '广德市', fullName: '广德市' },
    { province: '上海市', city: '上海市', district: '上海市', fullName: '上海市' },
    { province: '浙江省', city: '湖州市', district: '湖州市', fullName: '湖州市' },
    { province: '江苏省', city: '苏州市', district: '苏州市', fullName: '苏州市' },
    { province: '江苏省', city: '南京市', district: '南京市', fullName: '南京市' }
  ]
};

// 地区选择工具类
class RegionSelector {
  constructor() {
    this.provinces = regionData.provinces;
    this.cities = regionData.cities;
    this.districts = regionData.districts;
    this.commonAddresses = commonAddresses;
  }

  // 获取省份列表
  getProvinces() {
    return this.provinces.map(p => p.name);
  }

  // 根据省份获取城市列表
  getCitiesByProvince(provinceName) {
    const province = this.provinces.find(p => p.name === provinceName);
    if (!province) return [];
    
    const cities = this.cities[province.code] || [];
    return cities.map(c => c.name);
  }

  // 根据省份和城市获取区县列表
  getDistrictsByCity(provinceName, cityName) {
    const province = this.provinces.find(p => p.name === provinceName);
    if (!province) return [];
    
    const cities = this.cities[province.code] || [];
    const city = cities.find(c => c.name === cityName);
    if (!city) return [];
    
    const districts = this.districts[city.code] || [];
    return districts.map(d => d.name);
  }

  // 获取常见地址
  getCommonAddresses(type) {
    return this.commonAddresses[type] || [];
  }

  // 根据完整地址解析省市区
  parseAddress(fullAddress) {
    for (const province of this.provinces) {
      if (fullAddress.includes(province.name)) {
        const cities = this.cities[province.code] || [];
        for (const city of cities) {
          if (fullAddress.includes(city.name)) {
            const districts = this.districts[city.code] || [];
            for (const district of districts) {
              if (fullAddress.includes(district.name)) {
                return {
                  province: province.name,
                  city: city.name,
                  district: district.name,
                  fullName: fullAddress
                };
              }
            }
            return {
              province: province.name,
              city: city.name,
              district: '',
              fullName: fullAddress
            };
          }
        }
        return {
          province: province.name,
          city: '',
          district: '',
          fullName: fullAddress
        };
      }
    }
    return null;
  }

  // 构建完整地址
  buildFullAddress(province, city, district) {
    let fullAddress = province;
    if (city && city !== province) {
      fullAddress += city;
    }
    if (district && district !== city) {
      fullAddress += district;
    }
    return fullAddress;
  }
}

// 创建全局实例
const regionSelector = new RegionSelector();

module.exports = {
  regionData,
  commonAddresses,
  RegionSelector,
  regionSelector
};
